import {downloadUserInfo, downloadGroupInfo} from "../../api/im/msg";
import * as types from '../../store/types'
import {downLoadSession} from "../../api/im/msg";
import {downloadMsgList} from "../../api/im/msg";
import chatHandler from '../../handler/ChatHandler'
import MsgCenter from "../../util/socket/msg/MsgCenter";

export default {


    /**
     * 下载session的账户信息
     * @param commit
     * @param sessionList
     * @returns {Promise<void>}
     */
    async downLoadSessionInfo({commit, state}, sessionList) {
        if (!sessionList) return
        let userIds = []
        let groupIds = []
        //如果不存在在缓存 则下载队列
        for (let i = 0; i < sessionList.length; i++) {
            let session = sessionList[i]
            if (session.chatType == MsgCenter.chatType.GROUP) {

                if (!state.groupInfoMap[session.sessionId]) {
                    groupIds.push(session.sessionId)
                }
            } else {
                if (!state.userInfoMap[session.sessionId]) {
                    userIds.push(session.sessionId)
                }
            }
            if (!state.userInfoMap[session.targetId]) {
                userIds.push(session.targetId)
            }
        }
        //执行下载
        if (userIds.length > 0) {
            let res = await downloadUserInfo(userIds)
            commit(types.UPDATE_USER_INFO_MAP, res.data)
        }
        if (groupIds.length > 0) {
            let res = await downloadGroupInfo(groupIds)
            commit(types.UPDATE_GROUP_INFO_MAP, res.data)
        }
    },
    downLoadSession({commit, dispatch}) { //从服务器下载session
        downLoadSession().then(res => {
            let sessionList = res.data
            //异步下载session的账户信息
            dispatch('downLoadSessionInfo', sessionList)

            //更新数据session
            commit(types.UPDATE_SESSION_MAP, {
                type: 'merge',
                sessionList: sessionList
            })
        })
    },
    async downLoadCurrentUserMsgRecord({commit, state, dispatch}, {sessionId, chatType, init}) {
        let Limit = 36
        let firstMsgTime = new Date().getTime();
        let session = state.sessionMap[sessionId]
        if (session) {
            let currentMsgList = state.currentMsgList
            if (init && currentMsgList.length >= Limit) { // 或者记录已经超过限制也不下载
                return
            } else if (currentMsgList.length > 0) { //没有消息记录
                firstMsgTime = currentMsgList[0].createtime
            }
        }
        let res = await downloadMsgList(sessionId, chatType, firstMsgTime, Limit)
        let msgList = res.data
        if (msgList) {
            //消息记录倒插
            msgList = msgList.reverse()
            chatHandler.msgToLowerCase(msgList)
            commit(types.UPDATE_MSG_MAP, {
                type: 'merge',
                msgList: msgList
            })
            commit(types.UPDATE_CURRENT_MSG_LIST, {
                type: 'concat',
                msgList: msgList,
                sessionId: sessionId
            })
            let userIds = []
            //下载消息记录
            for (let i = 0; i < msgList.length; i++) {
                if (!state.userInfoMap[msgList[i].fromuserid]) {
                    userIds.push(msgList[i].fromuserid)
                }
                if (!state.userInfoMap[msgList[i].touserid]) {
                    userIds.push(msgList[i].touserid)
                }
                if(userIds.length>0){
                    downloadUserInfo(userIds).then(res=>{
                        commit(types.UPDATE_USER_INFO_MAP, res.data)
                    })
                }
            }
        }
    }
}